#!/bin/sh
# ===========================================
# Git commit-msg hook
# 验证提交消息是否符合 Conventional Commits 规范
# ===========================================

# 提交消息文件
commit_regex='^(feat|fix|docs|style|refactor|perf|test|chore|build|ci|revert)(\(.+\))?: .{1,50}'

# 读取提交消息
commit_message=$(cat "$1")

# 跳过合并提交
if echo "$commit_message" | grep -qE "^Merge (branch|pull request)"; then
    exit 0
fi

# 跳过回滚提交
if echo "$commit_message" | grep -qE "^Revert"; then
    exit 0
fi

# 验证提交消息格式
if ! echo "$commit_message" | grep -qE "$commit_regex"; then
    echo "❌ 提交消息格式不正确!"
    echo ""
    echo "正确格式: <type>(<scope>): <subject>"
    echo ""
    echo "类型 (type):"
    echo "  feat:     新功能"
    echo "  fix:      修复 bug"
    echo "  docs:     文档修改"
    echo "  style:    代码格式修改"
    echo "  refactor: 代码重构"
    echo "  perf:     性能优化"
    echo "  test:     测试用例修改"
    echo "  chore:    构建过程或辅助工具的变动"
    echo "  build:    影响构建系统或外部依赖的更改"
    echo "  ci:       持续集成配置文件和脚本的更改"
    echo "  revert:   回滚之前的提交"
    echo ""
    echo "示例:"
    echo "  feat(components): add new button component"
    echo "  fix(stores): resolve login state issue"
    echo "  docs(readme): update installation guide"
    echo ""
    echo "当前提交消息:"
    echo "  $commit_message"
    echo ""
    exit 1
fi

echo "✅ 提交消息格式正确"
exit 0 